package android.demo.activity.launchmode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.demo.activity.launchmode.singleinstance.SingleInstanceActivityA;
import android.demo.activity.launchmode.singletask.SingleTaskActivityA;
import android.demo.activity.launchmode.singletop.SingleTopActivityA;
import android.demo.activity.launchmode.standard.StandardActivityA;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class LaunchModeActivity extends ListActivity {
	
	private static final int STANDARD = 1;
	private static final int SINGLE_TOP = 2;
	private static final int SINGLE_TASK = 3;
	private static final int SINGLE_INSTANCE = 4;
	private ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
	
	{
		Map<String, Object> standard = new HashMap<String, Object>();  
		standard.put("text", "Standard");  
		standard.put("type", STANDARD);
		data.add(standard);  

		Map<String, Object> singleTop = new HashMap<String, Object>();
		singleTop.put("text", "SingleTop");  
		singleTop.put("type", SINGLE_TOP);
		data.add(singleTop);
		
		Map<String, Object> singleTask = new HashMap<String, Object>();
		singleTask.put("text", "SingleTask");  
		singleTask.put("type", SINGLE_TASK);
		data.add(singleTask); 
		
		Map<String, Object> singleInstance = new HashMap<String, Object>();
		singleInstance.put("text", "SingleInstance");  
		singleInstance.put("type", SINGLE_INSTANCE);
		data.add(singleInstance); 
	}
	
	public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState); 
		setTitle("@" + this.getTaskId());
		SimpleAdapter adapter = new SimpleAdapter(this, 
				data, 
				android.R.layout.two_line_list_item, 
				new String[] {"text"}, 
				new int[] {android.R.id.text1}
		);  
		setListAdapter(adapter); 
	}  
	
	protected void onListItemClick(ListView l, View v, int position, long id) {  
		super.onListItemClick(l, v, position, id);  
		int type = (Integer) data.get(position).get("type");
		switch (type) {
			case STANDARD :
				startActivity(new Intent(this, StandardActivityA.class));
				break;
			case SINGLE_TOP :
				startActivity(new Intent(this, SingleTopActivityA.class));
				break;
			case SINGLE_TASK :
				startActivity(new Intent(this, SingleTaskActivityA.class));
				break;
			case SINGLE_INSTANCE :
				startActivity(new Intent(this, SingleInstanceActivityA.class));
				break;
		}
	} 
}
